﻿
games.game8 = {
    //hangman
    words: "paris,newyork,london,frankfurt,harjit,dilshini,may,february,september,germany,paddington,plumstead,ealing,georgia,jackson,starbucks".split(","),
    currentWord: "",
    usedLetters: "",
    guesses: 0,
    errors: 0,
    init: function () {
        this.playRound();
        $(document).keyup(function (event) {
            var alphaKey = String.fromCharCode(event.keyCode).toLowerCase();
            if ("abcdefghijklmnopqrstuvwxyz".indexOf(alphaKey) > -1) {
                if (games.game8.usedLetters.indexOf(alphaKey) < 0) {
                    games.game8.guesses++;
                    games.game8.usedLetters += alphaKey;
                    $("#game8-hangman span span").each(function () {
                        var thisLetter = $(this).text();
                        if (thisLetter == alphaKey) {
                            $(this).css("visibility", "visible");
                        }
                    });
                    if (games.game8.currentWord.indexOf(alphaKey) < 0) {
                        games.game8.errors++;
                    }
                    var remaining = 0;
                    $("#game8-hangman span span").each(function () {
                        remaining = remaining + ($(this).css("visibility") == "hidden" ? 1 : 0);
                    });
                    if (remaining == 0) {
                        alert("well done, selecting another word");
                        games.game8.playRound();
                    }
                    if (games.game8.errors >= 10) {
                        alert("You loose, the word was..." + games.game8.currentWord + ". Never mind, play again!");
                        games.game8.playRound();
                    }
                    var livesBox = "";
                    for (var i = 0; i < games.game8.guesses - (games.game8.currentWord.length - remaining); i++) {
                        livesBox += " X";
                    }
                    $("#game8-hangman .lives").text(livesBox);
                    $("#game8-hangman .guesses").text(games.game8.usedLetters);
                }
            }
        });
    },
    playRound: function () {
        games.game8.usedLetters = "";
        games.game8.guesses = 0;
        games.game8.errors = 0;
        games.game8.currentWord = games.game3.words[games.helper.getRandomNumber(0, games.game3.words.length)];
        var html = "";
        html += "<div id='game8-hangman'><h1>Hang Man</h1>";
        for (var i = 0; i < games.game8.currentWord.length; i++) {
            html += "<span><span style='visibility:hidden'>{0}</span></span>".replace("{0}", games.game8.currentWord[i]);
        }
        html += "<div class='lives'></div>";
        html += "<div class='guesses'></div>";
        html += "</div>";
        $("#gamezone").html(html);
    }
};